﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataSeleced
{
    public partial class Default : System.Web.UI.Page
    {
        public SpiderHelper.Model.TempDoc doc = null;
        public int nowpage = 0;
        public int recordcount = 0;
        public int nowpagecount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
                ReSure r = new ReSure();
                r.Id = 1;

                string type = "0";
                if (!string.IsNullOrEmpty(Request.QueryString["type"]))
                {
                    type = Request.QueryString["type"];
                }
                if (type.Equals("0"))
                {
                    nowpage = db.GetModel(r).NCount;
                }
                else
                {
                    nowpage = db.GetModel(r).YCount;
                }

                recordcount = db.GetSingle("select count(*) from TempDoc where Sured=" + type);

                var query = db.ExecuteReader("select top 1 * from TempDoc where id > " + nowpage + " and Sured=" + type + " order by id asc");
                doc = new SpiderHelper.Model.TempDoc();
                while (query.Read())
                {
                    doc.Title = query["Title"].ToString();
                    doc.Content = query["Content"].ToString();
                    doc.Link = query["Link"].ToString();
                    doc.Id = Convert.ToInt32(query["Id"]);
                }
                this.HiddenField1.Value = doc.Id.ToString();
                this.newcontent.Value = doc.Content;
                query.Close();
                string sql = "select count(*) from TempDoc where Id<" + doc.Id + " and Sured=" + type;
                //   Response.Write(sql);
                nowpagecount = db.GetSingle(sql);
                nowpagecount = nowpagecount + 1;
                db.Commit();
                db.Dispose();
            }
        }
        /// <summary>
        /// 下一条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
            ReSure r = new ReSure();
            r.Id = 1;
            r = db.GetModel(r);
            string type = "0";
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                type = Request.QueryString["type"];
            }

            if (type.Equals("0"))
            {
                r.NCount = Convert.ToInt32(this.HiddenField1.Value);
            }
            else
            {
                r.YCount = Convert.ToInt32(this.HiddenField1.Value);
            }

            db.Edit(r);


            SpiderHelper.Model.TempDoc _doc = new SpiderHelper.Model.TempDoc();
            _doc.Id = Convert.ToInt32(this.HiddenField1.Value);
            _doc = db.GetModel(_doc);
            _doc.Sured = 1;
            db.Edit(_doc);


            db.Commit();
            db.Dispose();
            Response.Redirect("Default.aspx?type=" + type);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
            SpiderHelper.Model.TempDoc r = new SpiderHelper.Model.TempDoc();
            r.Id = Convert.ToInt32(this.HiddenField1.Value);
            r = db.GetModel(r);
            r.Sured = int.MinValue;
            db.Edit(r);
            db.Commit();
            db.Dispose();
            string type = "0";
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                type = Request.QueryString["type"];
            }

            Response.Redirect("Default.aspx?type=" + type);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
            SpiderHelper.Model.TempDoc r = new SpiderHelper.Model.TempDoc();
            r.Id = Convert.ToInt32(this.HiddenField1.Value);

            r = db.GetModel(r);
            r.Content = this.newcontent.Value.Replace("<wbr>", "").Replace("<wbr/>", "");
            db.Edit(r);
            db.Commit();
            db.Dispose();
            string type = "0";
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                type = Request.QueryString["type"];
            }
            Response.Redirect("Default.aspx?type=" + type);
        }
        /// <summary>
        /// 上一条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button4_Click(object sender, EventArgs e)
        {
            DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
            ReSure r = new ReSure();
            r.Id = 1;
            r = db.GetModel(r);
            string type = "0";
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                type = Request.QueryString["type"];
            }

            if (type.Equals("0"))
            {
                r.NCount = r.NCount - 1;
            }
            else
            {
                r.YCount = r.YCount - 1;
            }



            db.Edit(r);
            db.Commit();
            db.Dispose();
            Response.Redirect("Default.aspx?type=" + type);
        }

        /// <summary>
        /// 页面替换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button5_Click(object sender, EventArgs e)
        {
            DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
            SpiderHelper.Model.TempDoc r = new SpiderHelper.Model.TempDoc();
            r.Id = Convert.ToInt32(this.HiddenField1.Value);

            r = db.GetModel(r);
            r.Content = this.newcontent.Value.Replace(this.TextBox1.Text, this.TextBox2.Text).Replace("<wbr>", "").Replace("<wbr/>", "");
            db.Edit(r);
            db.Commit();
            db.Dispose();
            string type = "0";
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                type = Request.QueryString["type"];
            }
            Response.Redirect("Default.aspx?type=" + type);
        }
    }
}